<?php
/**
 * 账户安全控制器
 * @author e5
 *
 */
class SaveCenterController extends CommonController{
	private $db;
	public function __init(){
		parent::__init();
		$this->checkLogin();
		$this->db = K("User");
	}
	public function index() {
		//获得全部分类
		$cateData = K("Category")->all();
		//获得顶级分类
		$topCate = array();
		foreach ($cateData as $v){
			if ($v['pid'] == 0){
				$topCate[] = $v;
			}
		}
		$this->assign("topCate",$topCate);
		$this->assign("cateData",$cateData);
		$this->dis('saveCenter');
	}
	/**
	 * 修改密码
	 */
	public function editPwd(){
		if (IS_POST){
			$oldPwd = Q("post.oldPwd","");
			$newPwd = Q("post.newPwd","");
			if ($oldPwd == "") $this->error("请填写原密码");
			if ($newPwd == "") $this->error("请填写新密码");
			if ($newPwd != Q("post.newPwd1","")) $this->error("2次密码输入不一致");
			$oldPwd = md5($oldPwd);
			$newPwd = md5($newPwd);
			$uid = session("uid");
			if ($this->db->where("uid={$uid} AND upwd='{$oldPwd}'")->count() != 1) $this->error("原密码错误");
			if ($this->db->where("uid={$uid}")->update(array('upwd'=>$newPwd)) != 1) $this->error("修改密码失败");
			$this->success("修改密码成功",U("index"));
		}
		//获得全部分类
		$cateData = K("Category")->all();
		//获得顶级分类
		$topCate = array();
		foreach ($cateData as $v){
			if ($v['pid'] == 0){
				$topCate[] = $v;
			}
		}
		$this->assign("topCate",$topCate);
		$this->assign("cateData",$cateData);
		$this->dis('editPassword');
	}
	public function userInfo(){
		$uInfo = K("UserInfo");
		if (IS_POST){
			if (!$uInfo->editInfo()) $this->error($uInfo->error);
			$this->success("修改资料成功",U("Member/index"));
		}
		//获得全部分类
		$cateData = K("Category")->all();
		//获得顶级分类
		$topCate = array();
		foreach ($cateData as $v){
			if ($v['pid'] == 0){
				$topCate[] = $v;
			}
		}
		$this->assign("topCate",$topCate);
		$this->assign("cateData",$cateData);
		$uid = session("uid");
		$infoData = $uInfo->where("uid={$uid}")->find();
		$this->assign("infoData",$infoData);
		$this->dis('userInfo');
	}
}